package com.example.lin.jd_banner;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;

import com.example.lin.jd_banner.adapter.BannerPagerAdapter;
import com.example.lin.jd_banner.pojo.BannerData;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class BannerActivity extends AppCompatActivity {

    private ViewPager vp_banner;
    private BannerPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        initView();
        initDate();

    }

    @Override
    protected void onStart() {
        super.onStart();
        openLoop();
    }


    private void openLoop() {

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                while (true) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    publishProgress();
                }
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                int currentItem = vp_banner.getCurrentItem() + 1;

                vp_banner.setCurrentItem(currentItem % adapter.getCount());
            }
        }.execute();


    }

    private void initView() {
        vp_banner = findViewById(R.id.vp_banner);
        adapter = new BannerPagerAdapter(this);
        vp_banner.setAdapter(adapter);
    }

    private void initDate() {
        String address = "http://litchiapi.jstv.com/api/getTops?limit=5&column=0&val=F467412B44B421716757A6B2D7635B4A";
        new BannerWork().execute(address);
    }


    private class BannerWork extends AsyncTask<String, Void, BannerData> {

        @Override
        protected void onPostExecute(BannerData bannerData) {
            adapter.setData(bannerData);
        }

        @Override
        protected BannerData doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                int responseCode = conn.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String s = br.readLine();
                    return new Gson().fromJson(s, BannerData.class);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}
